//查询列表
import checkData from "./checkData";
import {isEmpty, isNotEmpty} from "../../../js/commonUtils";

export const initPageInfo = () => {
  checkData.tParam.searchParam.urls = "";

  checkData.tData.list1.splice(0);
  checkData.tData.list2.splice(0);
}

export const checkProducts = (self) => {
  if(isEmpty(checkData.tParam.searchParam.urls)) {
    self.$Message.error("采集链接不能为空！");
    return;
  }
  let prefixUrl = /^https?:\/\//;
  let prefix1688 = /^https:\/\/detail.1688.com/;
  let sep = "\n";
  let urlArr = checkData.tParam.searchParam.urls.split(sep);
  let urls = [];
  for(let url of urlArr) {
    url = url.trim();
    if(isNotEmpty(url)) {
      if(prefixUrl.test(url)) {
        if(prefix1688.test(url)) {
          url = url.indexOf("?")>0?url.substring(0, url.indexOf("?")):url;
        }
        urls.push(url);
      }
    }
  }
  if(urls.length == 0) {
    self.$Message.warning("输入内容无有效链接！");
    return;
  }
  if(urls.length > checkData.tData.maxUrlNum) {
    self.$Message.warning("单次检查链接数量不能大于"+checkData.tData.maxUrlNum+"！");
    return;
  }
  let params = {urls: urls};
  self.$Spin.show();
  self.$apiImpProduct.checkProducts(params).then(response => {
    self.$Spin.hide();
    checkData.tData.list1.splice(0);
    checkData.tData.list2.splice(0);
    if(response.success) {
      checkData.tData.list1.push(...response.result.list1);
      checkData.tData.list2.push(...response.result.list2);
      self.showTextArea = false;
    }else{
      self.$Message.error(response.errorMessage);
    }
  }).catch(() => {
    self.$Spin.hide();
    self.$Message.error("选品排查失败！");
  })
}
